package com.settlement.system.common.enums;

import com.settlement.system.common.util.common.DateUtil;
import com.settlement.system.model.entity.common.TblUnificationBillDetail;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * 统一账单问题描述
 *
 * @author Ximi
 * @since 2020/07/30
 */
public enum UnificationProblemDescEunm {

    /**
     * 支出金额与退款金额不一致
     */
    bill_amount(1, "支出金额与退款金额不一致"),
    /**
     * 原始账单项目为空
     */
    origin_bill_broject(2, "原始账单项目为空"),
    /**
     * 记账项目为空
     */
    record_account_project(4, "记账项目为空"),
    /**
     * 外部单号为空
     */
    third_order_no(8, "外部单号为空"),
    /**
     * 日期为空
     */
    out_date(16,"日期为空"),
    /**
     * 活动ID为空
     */
    action_id(32,"活动ID为空"),
    /**
     * 店铺为空
     */
    shop_no(64,"店铺为空"),
    /**
     * NC_店铺编码为空
     */
    nc_shop_no(128,"NC_店铺编码为空"),
    /**
     * 凭证类型为空
     */
    voucher_type(256,"凭证类型为空"),

    bill_category(512,"账单大类为空"),

    share_or_not(1024,"是否分摊为空"),

    before_or_after_ticket(2048,"票前票后为空"),
    nc_supplier_record(4096,"NC_辅助项_供应商档案为空");

    UnificationProblemDescEunm(int no, String name) {
        this.no = no;
        this.name = name;
    }

    private int no;
    private String name;


    public int getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

    /**
     * 编码
     * @param detail
     * @return
     */
    public static Integer encode(TblUnificationBillDetail detail) {

        Integer problemDesc = Integer.valueOf(0);
        final String yes = "Y";

        if (bill_amount.getName().equals(detail.getStatus())) {
            problemDesc |= bill_amount.getNo();
        }

        if(StringUtils.isEmpty(detail.getOriginBillProject())){
            problemDesc |= origin_bill_broject.getNo();
        }

        if (StringUtils.isEmpty(detail.getBillCategory())) {
            problemDesc |= bill_category.getNo();
        }

        if (StringUtils.isEmpty(detail.getShareOrNot())) {
            problemDesc |= share_or_not.getNo();
        }

        if (StringUtils.isEmpty(detail.getBeforeOrAfterTicket())) {
            problemDesc |= before_or_after_ticket.getNo();
        }

        if(yes.equals(detail.getIsJoinRecordAccount()) && StringUtils.isEmpty(detail.getRecordAccountProject()) ){
            problemDesc |= record_account_project.getNo();
        }

        if(yes.equals(detail.getIsJoinRecordAccount()) && StringUtils.isEmpty(detail.getVoucherType()) ){
            problemDesc |= voucher_type.getNo();
        }

        Date nullDate = DateUtil.parseToDate("2000-01-01 00:00:00","yyyy-MM-dd HH:mm:ss");
        boolean flag = detail.getOutDate() == null
                || (ChannelEnum.AKC.getNo().equals(detail.getChannelNo()) && detail.getOutDate().getTime() == nullDate.getTime());
        if(flag){
            problemDesc |= out_date.getNo();
        }

        if(yes.equals(detail.getAuditMark()) && StringUtils.isEmpty(detail.getThirdOrderNo())){
            boolean flag2 =
                (ChannelEnum.OR.getName().equals(detail.getChannelName())
                        || ChannelEnum.TM.getName().equals(detail.getChannelName())
                )
                && "收款-不能匹配到原始外部订单号".equals(detail.getOriginBillProject());
            if(flag2){
                ;
                //【统一账单】线下打款，状态逻辑优化
                //渠道为线下打款，且原始账单项目为[收款-不能匹配到原始外部订单号]，
                //如果外部订单号为空，则状态字段逻辑，不校验外部订单号是否为空。
            }else{
                problemDesc |= third_order_no.getNo();
            }
        }

        if(ChannelEnum.AKC.getNo().equals(detail.getChannelNo())
            && StringUtils.isEmpty(detail.getActionId())
            && !detail.getUnificationBillId().startsWith("MK-")
        ){
            problemDesc |= action_id.getNo();
        }

        if(StringUtils.isEmpty(detail.getShopNo())){
            if(ChannelEnum.OR.getName().equals(detail.getChannelName()) &&
                    ("收款-不能匹配到原始外部订单号".equals(detail.getOriginBillProject())
                            || "充值".equals(detail.getOriginBillProject())
                    )
            ){
                ;
                //【统一账单】线下打款，状态逻辑优化
                //渠道为线下打款，且原始账单项目为[收款-不能匹配到原始外部订单号]，
                //如果店铺或NC店铺为空，则状态字段逻辑，不校验店铺或NC店铺是否为空。
            }else{
                problemDesc |= shop_no.getNo();
            }
        }

        if(StringUtils.isEmpty(detail.getNcShopNo())){
            if(ChannelEnum.OR.getName().equals(detail.getChannelName()) &&
                    ("收款-不能匹配到原始外部订单号".equals(detail.getOriginBillProject())
                            || "充值".equals(detail.getOriginBillProject())
                    )
            ){
                ;
                //【统一账单】线下打款，状态逻辑优化
                //渠道为线下打款，且原始账单项目为[收款-不能匹配到原始外部订单号]，
                //如果店铺或NC店铺为空，则状态字段逻辑，不校验店铺或NC店铺是否为空。
            }else{
                problemDesc |= nc_shop_no.getNo();
            }
        }

        if(CostAllocationEnum.BeforeOrAfterTicketEnum.AFTER.getNo().equals(detail.getBeforeOrAfterTicket())){
            if(StringUtils.isEmpty(detail.getNcSupplierRecord())){
                problemDesc |= nc_supplier_record.getNo();
            }
        }

        return problemDesc;
    }

    /**
     * 解码
     * @param problemDesc
     * @return
     */
    public static String decode(Integer problemDesc){
        StringBuilder builder = new StringBuilder();
        for(UnificationProblemDescEunm eunm:UnificationProblemDescEunm.values()){
            if((problemDesc & eunm.getNo()) != 0){
                if(builder.length() > 0){
                    builder.append(",");
                }
                builder.append(eunm.getName());
            }
        }
        return builder.toString();
    }

}
